package cn.shengchao.qywx.remote.rpc;

import cn.hutool.core.collection.CollUtil;
import cn.shengchao.examstar.rpc.base.PageResult;
import cn.shengchao.examstar.rpc.distribute.enums.SysRoleDistributeBindEnum;
import cn.shengchao.examstar.rpc.wecom.api.EmployeeUserBindApi;
import cn.shengchao.examstar.rpc.wecom.req.EmployeeUserDto;
import cn.shengchao.examstar.rpc.wecom.req.EmployeeUserPageQuery;
import cn.shengchao.examstar.rpc.wecom.req.WeComUserQuery;
import cn.shengchao.examstar.rpc.wecom.req.WeComUserState;
import cn.shengchao.examstar.rpc.wecom.resp.EmployeeUserBindDetailVo;
import cn.shengchao.examstar.rpc.wecom.resp.EmployeeUserVo;
import cn.shengchao.qywx.infrastrure.po.QywxUser;
import cn.shengchao.qywx.infrastrure.po.QywxUserSysbind;
import cn.shengchao.qywx.infrastrure.service.QywxUserService;
import cn.shengchao.qywx.infrastrure.service.QywxUserSysbindService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: examstarapi
 * @description: 成员企微绑定
 * @author: Xiong weiqiang
 * @create: 2025-04-09 15:38
 */
@DubboService
@RequiredArgsConstructor
public class EmployeeUserBindDubboApi implements EmployeeUserBindApi {
    private final QywxUserService qywxUserService;
    private final QywxUserSysbindService qywxUserSysbindService;
    @Override
    public List<WeComUserState> listWeComUserByQuery(WeComUserQuery weComUserQuery) {
        List<WeComUserState> weComUserStateList=qywxUserService.selectWeComUserByQuery(weComUserQuery);
        return weComUserStateList;
    }

    @Override
    public EmployeeUserBindDetailVo getEmployeeUserBindDetail(Long sysUserId) {
        EmployeeUserBindDetailVo employeeUserBindDetailVo=qywxUserSysbindService.selectSysUserInfo(sysUserId);
        if (employeeUserBindDetailVo!=null){
            List<WeComUserState> list=new ArrayList<>();

            List<QywxUserSysbind> userSysbindList=qywxUserSysbindService.list(Wrappers.lambdaQuery(QywxUserSysbind.class).eq(QywxUserSysbind::getSysUserId,sysUserId));
            if (CollUtil.isNotEmpty(userSysbindList)){
                List<String> qywxUserIdList=userSysbindList.stream().map(QywxUserSysbind::getQywxUserId).toList();
                List<QywxUser> qywxUserList=qywxUserService.list(Wrappers.lambdaQuery(QywxUser.class).in(QywxUser::getUserId,qywxUserIdList));
                for (QywxUser qywxUser : qywxUserList) {
                    WeComUserState weComUserState=new WeComUserState();
                    weComUserState.setWeComUserId(qywxUser.getUserId());
                    weComUserState.setWeComUserName(qywxUser.getName());
                    weComUserState.setAsBind(1);
                    list.add(weComUserState);
                }
            }
            employeeUserBindDetailVo.setWeComUserStateList(list);
        }
        return employeeUserBindDetailVo;
    }
    @Transactional
    @Override
    public void saveUserWeComBind(EmployeeUserDto employeeUserDto) {
        qywxUserSysbindService.remove(Wrappers.lambdaQuery(QywxUserSysbind.class).eq(QywxUserSysbind::getSysUserId,employeeUserDto.getSysUserId()));
        List<QywxUserSysbind> saveList=new ArrayList<>();
        for (String weComUserId : employeeUserDto.getWeComUserIds()) {
            QywxUserSysbind qywxUserSysbind=new QywxUserSysbind();
            qywxUserSysbind.setSysUserId(employeeUserDto.getSysUserId());
            qywxUserSysbind.setQywxUserId(weComUserId);
            saveList.add(qywxUserSysbind);
        }
        qywxUserSysbindService.saveBatch(saveList);
    }
    @Transactional
    @Override
    public void delUserWeComBind(Long sysUserId) {
        qywxUserSysbindService.remove(Wrappers.lambdaQuery(QywxUserSysbind.class).eq(QywxUserSysbind::getSysUserId,sysUserId));
    }

    @Override
    public PageResult<EmployeeUserVo> queryUserWeComBind(EmployeeUserPageQuery employeeUserPageQuery) {
        Page<Object> page= PageHelper.startPage(employeeUserPageQuery.getPage(), employeeUserPageQuery.getSize());
        List<EmployeeUserVo> employeeUserVoList=qywxUserService.selectUserWeComBindPageList(employeeUserPageQuery);
        employeeUserVoList.forEach(employeeUserVo -> {
            if ("0".equals(employeeUserVo.getSysRoleId())){
                employeeUserVo.setSysRoleName("学员");
            }else {
                SysRoleDistributeBindEnum sysRoleDistributeBindEnum=SysRoleDistributeBindEnum.getByDistributeCode(employeeUserVo.getSysRoleId());
                if (sysRoleDistributeBindEnum!=null){
                    employeeUserVo.setSysRoleId(sysRoleDistributeBindEnum.getSysRoleId().toString());
                    employeeUserVo.setSysRoleName(sysRoleDistributeBindEnum.getDistributeName());
                }else {
                    employeeUserVo.setSysRoleName("学员");
                }
            }
        });
        return PageResult.of(page.getTotal(), employeeUserVoList);
    }
}